<?php

namespace App\Http\Controllers\v1;


use App\Models\Area;
use App\Models\Banner;
use App\Models\Family;
use App\Models\ClaimProject;
use App\Models\ClaimProjectCategory;
use App\Models\ClaimProjectServiceObj;
use App\Models\SmsCode;
use App\Models\Team;
use App\Models\User;
use App\Models\UserClaimProject;
use App\Models\UserCollection;
use App\Models\UserProject;
use App\Models\Volunteer;
use App\Models\VolunteerGoodAt;
use EasyWeChat\Core\Exception;
use Illuminate\Http\Request;
use App\Libs\IdCard;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Hashids\Hashids;

class ClaimController extends Controller
{

    public function listInfo(Request $request)
    {
        $pageNum = request()->input('current_page', 1);
        $pageSize = request()->input('page_size', 10);
        $from = ($pageNum - 1)*$pageSize;

        $nodes = ClaimProject::where('org_id', $this->getOrdId())->where('status', 1);
        $name = $request->get('name');
        if(!empty($name))//名称检索
        {
            $nodes = $nodes->where('name', 'like', "%{$name}%");
        }

        $count = $nodes->count();

        $nodes = $nodes->orderBy('publish_time','desc');

        $lists = $nodes->take($pageSize)->skip($from)->get();
        $list = [];
        foreach($lists as $value)
        {
            $tmp = $value->getAllInfo();
            $list[] = $tmp;
        }

        $pageInfo = [
            "current_page" => intval($pageNum),
            "total_page" => ceil($count / $pageSize),
            "page_size" => intval($pageSize)
        ];

        $data['list'] = $list;
        $data['page'] = $pageInfo;
        $this->setData($data);

        return $this->response();
    }


    public function info(Request $request, $id)
    {

        $node = ClaimProject::where('org_id', $this->getOrdId());

        $preview = $request->input('preview', 0);
        if($preview != 1)
        {
            $node = $node->where('status', 1);
        }
        $node = $node->where('id', $id);
        $node = $node->first();

        if(empty($node))
        {
            $this->setStatus(10001);
            $this->setMsg('参数错误,非法ID');
            return $this->response();
        }

        $data = $node->getAllInfo();

        $nodes = UserClaimProject::where('project_id', $id)->get();
        $data['claim_list'] = [];
        foreach($nodes as $value)
        {
            $data['claim_list'][] = UserClaimProject::format($value);
        }
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();
        $userClaimProject = UserClaimProject::where('project_id', $id)->where('user_id', $userInfo->id)->first();
        $data['join_status'] = empty($userClaimProject)? 0:1;



        $this->setData($data);
        return $this->response();
    }


    public function join(Request $request, $id)
    {
        $rules = ['team_name'=>'required', 'contact_name'=>'required', 'contact_phone'=>'required|is_phone', 'contact_email'=>'required|email', ];
        $validator = Validator::make(Input::all(), $rules);
        if($validator->fails())
        {
            $errors = $validator->errors();
            $this->setStatus(10001);
            $this->setMsg(implode(' ', $errors->all()));

            return $this->response();
        }
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();
        $userClaim = UserClaimProject::where('project_id', $id)->where('user_id', $userInfo->id)->first();
        if(!empty($userClaim))
        {
            $this->setStatus(10001);
            $this->setMsg('您已认领该项目');
            return $this->response();
        }

        $data = array_only($request->all(), ['team_name', 'contact_name', 'contact_email', 'contact_phone']);

        $data['project_id'] = $id;
        $data['user_id'] = $userInfo->id;
        $userClaim = new UserClaimProject();
        $userClaim->fill($data);

        $userClaim->save();

        $this->setMsg('认领成功');
        return $this->response();
    }

}
